/**
 * Created By: sisopipo Project Team
 * Created Date: Feb 25, 2011
 */
package com.sisopipo.assetsportal.webui.action.index;

import com.sisopipo.assetsportal.security.IOrganizationManager;
import com.sisopipo.assetsportal.security.domain.User;
import com.sisopipo.core.Constants;
import com.sisopipo.core.controller.struts.action.AnonymousAction;
import com.opensymphony.xwork2.ActionContext;

/**
 * @author ribin
 * @version 1.0
 */
@SuppressWarnings("unchecked")
public class LoginAction extends AnonymousAction {

	private IOrganizationManager organizationManager;

	private String userId;

	private String password;

	public String execute() throws Exception {
		User user = organizationManager.getUser(userId);
		if (user == null) {
			addActionError("login_user_not_found");
			return ERROR;
		}

		if (!user.authenticate(password)) {
			addActionError("login_auth_error");
			return ERROR;
		}

		ActionContext.getContext().getSession().put(Constants.LOGIN_USER_ID, userId);
		return SUCCESS;
	}

	public String getOriginalURL() {
		return (String) ActionContext.getContext().getSession().remove(Constants.ORIGINAL_URL);
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String loginame) {
		this.userId = loginame;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setOrganizationManager(IOrganizationManager userManager) {
		this.organizationManager = userManager;
	}

}
